<?php

namespace addons\shopro\controller;

use addons\shopro\model\User;
use think\Db;

class Ask extends Base
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = ['*'];

    //我的提问
    public function my_ask(){
        $user = $this->auth->getUser();

        $where = [
            'user_id' => $user->id
        ];
        $ask=Db::name('ask')->alias('a')
            ->join('ask_desc ad','ad.ask_id=a.id','left')
            ->where($where)
            ->field('a.id,a.title,a.count,ad.name as d_name,ad.image as d_image,ad.title as d_title')
            ->order('a.weigh desc')
            ->select();

        $data=array();
        foreach ($ask as $v){
            $i=0;
            foreach ($data as $key=>$d){
                if($d['id']==$v['id']){
                    $desc_info=array();
                    $desc_info['d_title']=$v['d_title'];
                    $desc_info['d_name']=$v['d_name'];
                    $desc_info['d_image']=$v['d_image'];
                    $data[$key]['desc_list'][]=$desc_info;
                    $i=1;
                }
            }
            if($i==0){
                $info=array();
                $info['id']=$v['id'];
                $info['title']=$v['title'];
                $info['count']=$v['count'];
                $info['is_show']=0;
                $info['desc_list']=array();
                if(!empty($v['d_title'])){
                    $desc_info=array();
                    $desc_info['d_title']=$v['d_title'];
                    $desc_info['d_name']=$v['d_name'];
                    $desc_info['d_image']=$v['d_image'];
                    $info['desc_list'][]=$desc_info;
                }
                $data[]=$info;
            }
        }

        $this->success('我的提问', $data);
    }
}